package com.sec.etech.form.rest.controller;

import com.sec.etech.form.rest.service.EtechFormCustDialogManager;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.core.util.ThreadMapUtil;
import org.openbpm.base.db.datasource.DbContextHolder;
import org.openbpm.base.db.model.page.PageResult;
import org.openbpm.form.model.FormCustDialog;
import org.openbpm.form.rest.controller.FormCustDialogController;
import org.openbpm.sys.api.model.ISysDataSource;
import org.openbpm.sys.api.service.ISysDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/etech/formCustDialog/"})
public class EtechFormCustDialogController extends FormCustDialogController {

    @Autowired
    EtechFormCustDialogManager formCustDialogManager;
    @Autowired
    ISysDataSourceService sysDataSourceService;

    @RequestMapping({"listData_{key}"})
    @CatchErr(
            write2response = true,
            value = "获取对话框的列表数据失败"
    )
    public PageResult listData(HttpServletRequest request, HttpServletResponse response, @PathVariable("key") String key) throws Exception {
        boolean isPage = request.getParameter("limit") != null;
        ThreadMapUtil.put("isPage", isPage);
        FormCustDialog formCustDialog = this.formCustDialogManager.getByKey(key);
        QueryFilter queryFilter = this.getQueryFilter(request);
        ISysDataSource sysDataSource = this.sysDataSourceService.getByKey(formCustDialog.getDsKey());
        if (sysDataSource != null) {
            DbContextHolder.setDataSource(sysDataSource.getKey(), sysDataSource.getDbType());
        }

        List<?> list = this.formCustDialogManager.data(formCustDialog, queryFilter);
        ThreadMapUtil.remove();
        return new PageResult(list);
    }
}
